﻿@model IEnumerable<Superstore.Vender.Models.ShoppingCartGoodsInfo>
@foreach (var item in Model)
{
    if (1 == item.out_stack)
    {
        <tr id="@item.goods_info.goodsid">
            <td class="shopping-goods-name">@item.goods_info.name</td>
            @{decimal goodsTotalPrice = @item.goods_num * @item.goods_info.price;}
            <td class="shopping-goods-price" id="total_money_@item.goods_info.goodsid">￥@goodsTotalPrice.ToString("#0.00")</td>
            <td class="shopping-goods-count">
                <section class="IndexGoodsNum">
                    <a class="dec_count_@item.goods_info.goodsid OrderPlus" onclick="adjustNum(@item.goods_info.goodsid, @item.main_tab, @item.sub_tab, -1)">
                        <img src="/images/Dec.png" ontouchstart="replaceAddOrDecImage(this, 2)" ontouchend="replaceAddOrDecImage(this, 4)" />
                    </a>
                    <span id="goodsid_@item.goods_info.goodsid">@item.goods_num</span>
                    <a class="add_count_@item.goods_info.goodsid OrderAdd" onclick="adjustNum(@item.goods_info.goodsid, @item.main_tab, @item.sub_tab, 1)">
                        <img src="/images/Add.png" ontouchstart="replaceAddOrDecImage(this, 1)" ontouchend="replaceAddOrDecImage(this, 3)" />
                    </a>
                </section>
            </td>
        </tr>
    }
    else
    {
        //缺货
        <tr id="@item.goods_info.goodsid">
            <td class="shopping-goods-name">@item.goods_info.name</td>
            @{decimal goodsTotalPrice = @item.goods_num * @item.goods_info.price;}
            <td class="shopping-goods-price" id="total_money_@item.goods_info.goodsid">￥@goodsTotalPrice.ToString("#0.00")</td>
            <td class="shopping-goods-count">
                <section class="IndexGoodsNum">
                    <a class="dec_count_@item.goods_info.goodsid OrderPlus" onclick="adjustNum(@item.goods_info.goodsid, @item.main_tab, @item.sub_tab, -1)"><img src="/images/Dec.png"></a>
                    <span id="goodsid_@item.goods_info.goodsid">@item.goods_num</span>
                    <a class="add_count_@item.goods_info.goodsid OrderAdd" onclick="adjustNum(@item.goods_info.goodsid, @item.main_tab, @item.sub_tab, 1)"><img src="/images/Add.png"></a>
                </section>
                </td>
            </tr>
        <tr class="short_goods_shadow"></tr>
    }
}

<script>
    $(function () {
        adjustGoodsList();
    });

    function adjustGoodsList() {
        var shoppingList = ShoppingMgr.getShoppingList();
        $('.IndexGoodsNum').each(function (index) {
            var goodsNum = Number(shoppingList[index].goods_num);
            if (goodsNum <= 0) {
                $('.IndexGoodsNum a').eq(2 * index).hide();
                $('.IndexGoodsNum span').eq(index).hide();
            } else {
                $('.IndexGoodsNum a').eq(2 * index).show();
                $('.IndexGoodsNum span').eq(index).show();
            }
        });
    }

    function adjustGoodsListShow(goodsid, price, goodsNum) {
        if (goodsNum > 0) {
            $(".dec_count_" + goodsid).show();
            $("#goodsid_" + goodsid).show();
            $("#goodsid_" + goodsid).text(goodsNum);
            var totalMoney = Number(price) * Number(goodsNum);
            $("#total_money_" + goodsid).text("￥" + totalMoney.toFixed(2));
            showShoppingCarFee();
        } else {
            $(".dec_count_" + goodsid).hide();
            $("#goodsid_" + goodsid).hide();
        }
    }

    function adjustGoodsNumShow(goodsid, goodsNum, shoppingList) {
        if (goodsNum <= 0) {
            $("#" + goodsid).hide();
            showShoppingCarFee();
        } else {
            for (index in shoppingList) {
                if (goodsid == shoppingList[index].goods_info.goodsid) {
                    adjustGoodsListShow(goodsid, shoppingList[index].goods_info.price, goodsNum);
                }
            }
        }
    }

    function adjustNum(goodsId, mainTab, subTab, typeOp) {
        ShoppingMgr.adjustGoodsNum(goodsId, mainTab, subTab, typeOp, adjustGoodsNumShow);
    }
</script>
